package com.controller;

import com.entity.Admin;
import com.entity.Company;
import com.entity.User;
import com.service.LoginService;
import com.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/CheckLoginServlet")
public class CheckLoginServlet extends HttpServlet {

    private LoginService loginService= new LoginServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String name=req.getParameter("name");
        String pwd=req.getParameter("pwd");
        String entity=req.getParameter("entity");
        System.out.println("entity = " + entity);
//        System.out.println("管理员".equals(entity));
        if ("管理员".equals(entity)){
            Admin admin;

            admin=loginService.selectAllAdmin(name,pwd);

            if (admin!=null){
                HttpSession session = req.getSession();
                session.setAttribute("admin", admin);
                System.out.print(admin);
                resp.getWriter().write("success");
            }else {
                resp.getWriter().write("fail");
            }
        } else if ("求职者".equals(entity)){
            User user;
            user=loginService.selectAllUser(name,pwd);
            if (user!=null){
                HttpSession session = req.getSession();
                session.setAttribute("user", user);
                System.out.print(user);
                resp.getWriter().write("success");
            }else {
                resp.getWriter().write("fail");
            }

        }else if("招聘者".equals(entity)){
            Company company;
            company=loginService.selectAllCompany(name,pwd);
            if (company!=null){
                HttpSession session = req.getSession();
                session.setAttribute("company", company);
                System.out.print(company);
                resp.getWriter().write("success");
            }else {
                resp.getWriter().write("fail");
            }

        }else {
            resp.getWriter().write("fail");
        }
    }
}
